﻿Public Class AlertGroupUI


    'いろいろテストしながらやってます。
    'コード整理は後でやりますので～


    ''値を返す
    'Public Property GetClass() As String

    'Get
    'Return "test"
    'End Get
    'Set(ByVal value As String)

    'End Set
    'End Property

#Region "グリッドビューのインデックス、設定記憶用変数"
    'インデックスを覚える
    Dim DataGridView_Right_IndexRemember As Integer
    Dim DataGridView_Left_IndexRemember As Integer
    '削除可能かどうか
    Dim CanDelete As Boolean = False
    '名前を一時的に覚えておく
    Dim SettingNameRemember As String
#End Region


    'とりあえずMonoのなんとか?
    Private ReadOnly Property IsRunnungOnMono() As Boolean
        Get
            Return Type.GetType("Mono.Runtime") IsNot Nothing
        End Get
    End Property

    Private Sub AlertGroupUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        leftDataGridView.AutoGenerateColumns = False
        leftDataGridView.Rows.Add()
        leftDataGridView("SettingName", leftDataGridView.Rows.Count - 1).Value = "既定の設定"
        leftDataGridView("SettingFlag", 0).Value = True
        leftDataGridView("SettingFlag", 0).Value = True
        leftDataGridView("SettingFlag", 0).ReadOnly = True '設定1は変更不可能
        leftDataGridView.DefaultCellStyle.NullValue = "値が設定されていません" 'このメッセージは本来表示されないはず....
    End Sub

    'OKボタンをおしたときの処理
    Private Sub Button_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click

    End Sub

#Region "保留中(勉強中)"

    'Denebのアレを参考にする予定(今のところコード持って来てる)
    Private Sub DataGridView_Left_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles leftDataGridView.RowPrePaint
        If (e.PaintParts And DataGridViewPaintParts.Background) = 0 AndAlso
            (e.PaintParts And DataGridViewPaintParts.ContentForeground) = 0 Then
            Exit Sub
        End If

        '背景
        'しましま模様になる
        Dim backColor As Color
        If (e.PaintParts And DataGridViewPaintParts.SelectionBackground) > 0 AndAlso
            (e.State And DataGridViewElementStates.Selected) > 0 Then
            backColor = Color.FromArgb(235, 235, 235)
        Else
            backColor = Color.White
        End If

        If (e.PaintParts And DataGridViewPaintParts.Background) > 0 Then
            Dim width As Integer
            If IsRunnungOnMono Then
                width = e.RowBounds.Width
            Else
                width = leftDataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
            End If

            Using b = New SolidBrush(backColor)
                e.Graphics.FillRectangle(Brushes.AliceBlue, New RectangleF(
                                         e.RowBounds.X, e.RowBounds.Y,
                                         width, e.RowBounds.Height))
            End Using
        End If

    End Sub

    Private Sub DataGridView_Left_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles leftDataGridView.RowPostPaint
        Using _pen = New Pen(Color.LightGray)
            e.Graphics.DrawLine(_pen, e.RowBounds.X, e.RowBounds.Y + e.RowBounds.Height - 1, e.RowBounds.X + e.RowBounds.Width, e.RowBounds.Y + e.RowBounds.Height - 1)
        End Using
    End Sub

#End Region

#Region "追加ボタン、削除ボタン"
    Private Sub Button_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
        Dim CountNum As Integer = 0
        Dim IndexNum As Integer = 1

        Dim Strs As String

        Do While 1
            Strs = leftDataGridView("SettingName", CountNum).Value
            If Strs = "設定" + IndexNum.ToString Then
                CountNum = 0 '名前の重複があったのでもう一度探し直し
                IndexNum += 1 '1足した数字の名前がおｋか調べる
            End If
            CountNum += 1
            If CountNum > leftDataGridView.Rows.Count - 1 Then
                'カウントが0にならない且つ行の数より多い == 一致するものがなかった
                Exit Do
            End If
        Loop
        leftDataGridView.Rows.Add()
        leftDataGridView("SettingName", leftDataGridView.Rows.Count - 1).Value = "設定" + IndexNum.ToString
    End Sub

    Private Sub Button_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click
        '既定の設定の場合は削除できない
        If leftDataGridView("SettingName", leftDataGridView.SelectedRows.Item(0).Index).Value = "既定の設定" Then
            Exit Sub
        End If
        leftDataGridView.Rows.Remove(leftDataGridView.SelectedRows.Item(0))
    End Sub

#End Region

#Region "左側のデータ選択された時の右側への表示"

    'デバッグ用に今は時分でデータを作って処理をしているが
    '後で渡されたデータを元に行を追加するような記述にしたい

    Private Sub DataGridView_Left_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftDataGridView.SelectionChanged
        '選択された時の処理
        '選択されたら右側のグリッドビューに表示&テキストボックス変更
        'TextBox_SettingName.Text = DataGridView_Left("SettingName", e.RowIndex).Value
    End Sub

    Sub DataGridView_Right_DataShow()
        rightDataGridView.Rows.Clear()
        rightDataGridView.Rows.Add()
        rightDataGridView.Rows.Add()
        rightDataGridView.Rows.Add()
        rightDataGridView.Rows.Add()

        'Sound設定
        rightDataGridView(0, 0).Value = leftDataGridView("Sound", leftDataGridView.SelectedRows.
                                                           Item(0).Index).Value
        rightDataGridView(2, 0).Value = "Sound"
        'Mail設定
        rightDataGridView(0, 1).Value = leftDataGridView("Mail", leftDataGridView.SelectedRows.
                                                           Item(0).Index).Value
        rightDataGridView(2, 1).Value = "Mail"

        'Baloon設定
        rightDataGridView(0, 2).Value = leftDataGridView("Baloon", leftDataGridView.SelectedRows.
                                                           Item(0).Index).Value
        rightDataGridView(2, 2).Value = "Baloon"
1:
        'Brawser設定
        rightDataGridView(0, 3).Value = leftDataGridView("Browser", leftDataGridView.SelectedRows.
                                                           Item(0).Index).Value
        rightDataGridView(2, 3).Value = "Browser"
    End Sub
#End Region


    Private Sub DataGridView_Left_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles leftDataGridView.CellEnter
        'セルがクリックされた時の処理
        '起動時は 0 個なのでエラーがでる -> try - chatch
        Try
            'テキストボックスの内容変更
            TextBox_SettingName.Text = leftDataGridView("SettingName", leftDataGridView.SelectedRows.Item(0).Index).Value.ToString
            '名前の重複や完全空白の場合に設定名を戻せるようにしておく・・・設定名の記憶
            SettingNameRemember = TextBox_SettingName.Text

            'インデックスを記憶しておく
            DataGridView_Left_IndexRemember = leftDataGridView.SelectedRows.Item(0).Index


            '既定の設定だったら名称変更を禁止する
            If leftDataGridView("SettingName", leftDataGridView.SelectedRows.Item(0).Index).Value = "既定の設定" Then
                TextBox_SettingName.ReadOnly = True
            Else
                TextBox_SettingName.ReadOnly = False
            End If

            DataGridView_Right_DataShow()

        Catch ex As Exception

        End Try

    End Sub

    Sub DataGridView_CellWrite()
        leftDataGridView(rightDataGridView("通知方法名称", DataGridView_Right_IndexRemember).Value.ToString,
                      leftDataGridView.SelectedRows.Item(0).Index).Value = rightDataGridView(0, DataGridView_Right_IndexRemember).Value

    End Sub

    Private Sub DataGridView_Right_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles rightDataGridView.CellEndEdit
        DataGridView_CellWrite()

    End Sub

    Private Sub DataGridView_Right_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles rightDataGridView.CellBeginEdit
        DataGridView_Right_IndexRemember = rightDataGridView.SelectedRows.Item(0).Index
    End Sub

    Private Sub TextBox_SettingName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox_SettingName.TextChanged
        '設定名変更処理

        '空白だったら元の名前に戻す
        'If TextBox_SettingName.Text = "" Then
        'TextBox_SettingName.Text = SettingNameRemember
        'MsgBox("設定名が不正です。空白のみにしないでください。")
        'End If

        leftDataGridView("SettingName", leftDataGridView.SelectedRows.Item(0).Index).Value = TextBox_SettingName.Text.ToString


    End Sub

#Region "設定名変更"
    Private Sub TextBox_SettingName_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox_SettingName.Leave
        '設定名変更処理

        '空白文字、タブを除去して検査
        Dim str As String = TextBox_SettingName.Text
        str = Replace(str, " ", "")
        str = Replace(str, "　", "")
        str = Replace(str, "\t", "")

        '空白だったら元の名前に戻す
        If str = "" Then
            TextBox_SettingName.Text = SettingNameRemember
            'エラー用の記述
            'MsgBox("設定名が不正です。空白のみにしないでください。")
        End If

        leftDataGridView("SettingName", DataGridView_Left_IndexRemember).Value = TextBox_SettingName.Text.ToString


    End Sub
#End Region

    Private Sub CopyDataGridView(ByVal a As Integer, ByVal b As Integer)
        'コピー関数

        'Insertした後に行番号が1つずれるのを修正
        Dim num As Integer = 0
        If a > b Then
            num = 1

        End If

        leftDataGridView.Rows.Insert(b, 1)
        Dim count As Integer = 0
        While (count < leftDataGridView.Columns.Count)
            leftDataGridView(count, b).Value = leftDataGridView(count, a + num).Value
            count += 1
        End While
    End Sub

    Private Sub Button_Up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upButton.Click
        '一番上は交換不可
        If leftDataGridView.SelectedRows.Item(0).Index < 2 Then
            Exit Sub
        End If

        Dim Index As Integer = leftDataGridView.SelectedRows.Item(0).Index

        CopyDataGridView(Index, Index - 1)
        leftDataGridView.Rows.RemoveAt(Index + 1)
        leftDataGridView.Rows(Index - 1).Selected = True
    End Sub

    Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click
        '一番下と一番上は交換不可
        If leftDataGridView.SelectedRows.Item(0).Index = leftDataGridView.RowCount - 1 Or leftDataGridView.SelectedRows.Item(0).Index = 0 Then
            Exit Sub
        End If

        Dim Index As Integer = leftDataGridView.SelectedRows.Item(0).Index

        CopyDataGridView(Index, Index + 2)
        leftDataGridView.Rows.RemoveAt(Index)
        leftDataGridView.Rows(Index + 1).Selected = True

    End Sub
End Class
